# task is in a format of
# {
#     'comic_name': {
#         "chapter_name": {
#             "pic name without appendix": "pic url"
#         }
#     }
# }



import json
import time
from concurrent.futures.thread import ThreadPoolExecutor

import save_util

def download_thread(comic_name, chapter_name, image_name, image_url):
    result = True
    try:
        save_util.save_img_without_appendix(comic_name, chapter_name, image_name, image_url)
    except Exception as e:
        print(e)
        print('Task failed, Comic name:\t' + comic_name + '\tChapter name:\t' + chapter_name + '\tImage info:\t' + image_name + ':' + image_url)
        result = False
    return [result, comic_name, chapter_name, image_name, image_url]

task_file = open('./task.json', 'r', encoding='UTF8')
download_task = dict(json.load(task_file))
task_file.close()
comic_names = list(download_task.keys())
for comic_name in comic_names:
    chapter_names = list(download_task[comic_name].keys())
    for chapter_name in chapter_names:
        if len(download_task[comic_name][chapter_name]) == 0:
            download_task[comic_name].pop(chapter_name)

for comic_name in comic_names:
    if len(download_task[comic_name]) == 0:
        del download_task[comic_name]

task_file = open('./task.json', 'w', encoding='UTF8')
json_data = json.dumps(download_task, ensure_ascii=False)
task_file.write(json_data)
task_file.close()


for comic_name in comic_names:
    chapter_names = download_task[comic_name].keys()
    count = 0
    print('Start task on comic: \t' + comic_name)
    for chapter_name in chapter_names:
        count = count + 1

        with ThreadPoolExecutor(max_workers=10) as executor:
            futures = []
            failed_tasks = {}
            for image_name in download_task[comic_name][chapter_name].keys():
                image_url = download_task[comic_name][chapter_name][image_name]
                future = executor.submit(download_thread, comic_name, chapter_name, image_name, image_url)
                futures.append(future)
            for future in futures:
                [result, comic_name, chapter_name, image_name, image_url] = future.result()
                if not result:
                    failed_tasks[image_name] = image_url
            download_task[comic_name][chapter_name] = failed_tasks
        task_file = open('./task.json', 'w', encoding='UTF8')
        json_data = json.dumps(download_task, ensure_ascii=False)
        task_file.write(json_data)
        task_file.close()
        print('Comic name:\t' + comic_name + '\tChapter process:\t' + str(count) + '/' + str(len(chapter_names)))

